package com.family.base.util;

import java.util.List;
import java.util.Map;

public class Pager<T> {

	private Integer totalRows;
	private Integer pageSize = Integer.valueOf(10);
	private Integer currentPage;
	private Integer totalPages;
	private Integer startRow;
	private String jsonWhere;
	private String[] excludes;
	private Integer sreachType = Integer.valueOf(1);
	private List<T> result;
	private Map<String, Number> groupValue;

	public void init(Integer _totalRows) {
		this.totalRows = _totalRows;
		this.totalPages = Integer.valueOf(this.totalRows.intValue() / this.pageSize.intValue());
		Integer mod = Integer.valueOf(this.totalRows.intValue() % this.pageSize.intValue());
		if (mod.intValue() > 0) {
			Pager<T> tmp_ = this;
			tmp_.totalPages = Integer.valueOf(tmp_.totalPages.intValue() + 1);
		}
		if (this.currentPage == null) {
			this.currentPage = Integer.valueOf(1);
		}
		this.startRow = Integer.valueOf(0);
	}

	public boolean isPagerSreach() {
		return (this.sreachType.intValue() == 2);
	}

	public Integer getStartRow() {
		return this.startRow;
	}

	public Integer getTotalPages() {
		return this.totalPages;
	}

	public Integer getCurrentPage() {
		return this.currentPage;
	}

	public Integer getPageSize() {
		return this.pageSize;
	}

	public void setTotalRows(Integer totalRows) {
		this.totalRows = totalRows;
	}

	public void setStartRow(Integer startRow) {
		this.startRow = startRow;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalRows() {
		return this.totalRows;
	}

	public List<T> getResult() {
		return this.result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public String getJsonWhere() {
		return this.jsonWhere;
	}

	public void setJsonWhere(String jsonWhere) {
		this.jsonWhere = jsonWhere;
	}

	public Integer getSreachType() {
		return this.sreachType;
	}

	public void setSreachType(Integer sreachType) {
		this.sreachType = sreachType;
	}

	public String[] getExcludes() {
		return this.excludes;
	}

	public void setExcludes(String[] excludes) {
		this.excludes = excludes;
	}

	public Map<String, Number> getGroupValue() {
		return this.groupValue;
	}

	public void setGroupValue(Map<String, Number> groupValue) {
		this.groupValue = groupValue;
	}

	public void first() {
		this.currentPage = Integer.valueOf(1);
		this.startRow = Integer.valueOf(0);
	}

	public void previous() {
		if (this.currentPage.intValue() == 1)
			return;
		Pager<T> tmp_ = this;
		tmp_.currentPage = Integer.valueOf(tmp_.currentPage.intValue() - 1);
		this.startRow = Integer.valueOf((this.currentPage.intValue() - 1) * this.pageSize.intValue());
	}

	public void next() {
		if (this.currentPage.intValue() < this.totalPages.intValue()) {
			Pager<T> tmp_ = this;
			tmp_.currentPage = Integer.valueOf(tmp_.currentPage.intValue() + 1);
		}
		this.startRow = Integer.valueOf((this.currentPage.intValue() - 1) * this.pageSize.intValue());
	}

	public void last() {
		this.currentPage = this.totalPages;
		this.startRow = Integer.valueOf((this.currentPage.intValue() - 1) * this.pageSize.intValue());
	}

	public void refresh(Integer _currentPage) {
		this.currentPage = _currentPage;
		if (this.currentPage.intValue() < this.totalPages.intValue())
			last();
	}

	public void refresh() {
		if (this.currentPage.intValue() < this.totalPages.intValue())
			this.startRow = Integer.valueOf((this.currentPage.intValue() - 1) * this.pageSize.intValue());
		else
			last();
	}

	public Integer getIndex(Integer index) {
		return Integer.valueOf((this.currentPage.intValue() - 1) * this.pageSize.intValue() + index.intValue() + 1);
	}
}
